package o;

import android.content.Context;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.text.TextUtils;
import androidx.annotation.NonNull;
import com.huawei.appmarket.service.appmgr.bean.ApkInstalledInfo;
import com.huawei.hms.framework.network.restclient.hwhttp.dns.DnsConfig;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import o.epx.a;

public class dtd {
    private static final Object ˏ = new Object();
    private static dtd ॱ;
    private List<ApkInstalledInfo> ˊ = null;
    private dtf ˋ;
    private b ˎ = new dsy();

    public interface b {
        void ˊ(@NonNull String str, int i);
    }

    public static dtd ˎ() {
        dtd o_dtd;
        synchronized (ˏ) {
            if (ॱ == null) {
                ॱ = new dtd();
            }
            o_dtd = ॱ;
        }
        return o_dtd;
    }

    public synchronized void ˏ(List<ApkInstalledInfo> list) {
        if (this.ˊ == null) {
            this.ˊ = new ArrayList();
        }
        if (list != null) {
            this.ˊ.clear();
            this.ˊ.addAll(list);
            Logger.ˎ("InstalledAppDataMgr", "refreshInstalledAppList: " + this.ˊ.size());
        }
    }

    public synchronized boolean ˏ() {
        return this.ˊ != null;
    }

    public synchronized List<ApkInstalledInfo> ˊ() {
        List<ApkInstalledInfo> list;
        if (CollectCheck.isEmpty(this.ˊ)) {
            Logger.ˎ("InstalledAppDataMgr", "getInstalledAppList is empty!");
            list = null;
        } else {
            List<ApkInstalledInfo> arrayList = new ArrayList(this.ˊ);
            if (!(this.ˋ == null || arrayList.isEmpty())) {
                for (ApkInstalledInfo apkInstalledInfo : this.ˊ) {
                    if (this.ˋ.ˋ(apkInstalledInfo)) {
                        arrayList.remove(apkInstalledInfo);
                    }
                }
            }
            list = arrayList;
        }
        return list;
    }

    public synchronized ApkInstalledInfo ॱ(String str) {
        ApkInstalledInfo apkInstalledInfo;
        if (TextUtils.isEmpty(str) || this.ˊ == null) {
            Logger.e("InstalledAppDataMgr", "getInstalledApp, packageName = " + str);
            apkInstalledInfo = null;
        } else {
            for (ApkInstalledInfo apkInstalledInfo2 : this.ˊ) {
                if (str.equals(apkInstalledInfo2.ॱ())) {
                    break;
                }
            }
            apkInstalledInfo2 = null;
        }
        return apkInstalledInfo2;
    }

    synchronized void ˎ(String str) {
        if (this.ˊ == null) {
            Logger.e("InstalledAppDataMgr", "removeInstalledApp, installedAppList == null");
        } else {
            Context ˋ = ContentManager.getInstant().getContext();
            PackageManager packageManager = ˋ.getPackageManager();
            ApkInstalledInfo ॱ = ॱ(str);
            if (ॱ != null) {
                if (eqh.ˎ().ˏ()) {
                    int i = DnsConfig.MAX_CACHE_ENTRIES;
                    try {
                        boolean ˋ2 = eqh.ˎ().ˋ();
                        if (ˋ2) {
                            i = 8320;
                        }
                        ApplicationInfo applicationInfo = packageManager.getApplicationInfo(str, i);
                        if (ˋ2 && applicationInfo != null && (applicationInfo.flags & 8388608) == 0) {
                            ॱ.ˊ(false);
                        }
                    } catch (NameNotFoundException e) {
                        if (Logger.ˎ()) {
                            Logger.ˏ("InstalledAppDataMgr", "app not install in this device");
                        }
                    }
                }
                this.ˊ.remove(ॱ);
            }
            ll.ˏ(ˋ).ˋ(new Intent(a.ˋ));
        }
    }

    synchronized void ˏ(ApkInstalledInfo apkInstalledInfo) {
        if (apkInstalledInfo != null) {
            if (!TextUtils.isEmpty(apkInstalledInfo.ॱ())) {
                if (this.ˊ == null) {
                    Logger.e("InstalledAppDataMgr", "the installed app list is not initial yet. it's useless to update!");
                } else {
                    Iterator it = this.ˊ.iterator();
                    while (it.hasNext()) {
                        ApkInstalledInfo apkInstalledInfo2 = (ApkInstalledInfo) it.next();
                        if (apkInstalledInfo2 != null && apkInstalledInfo.ॱ().equals(apkInstalledInfo2.ॱ())) {
                            it.remove();
                        }
                    }
                    this.ˊ.add(apkInstalledInfo);
                    ll.ˏ(ContentManager.getInstant().getContext()).ˋ(new Intent(a.ˎ));
                    Logger.ˎ("InstalledAppDataMgr", "updateInstalledApp pkg :" + apkInstalledInfo.ॱ());
                }
            }
        }
        Logger.e("InstalledAppDataMgr", "updateInstalledApp app is null");
    }

    synchronized void ˊ(String str, int i) {
        if (TextUtils.isEmpty(str)) {
            Logger.ˎ("InstalledAppDataMgr", "the package is empty when updateInstalledGameSp");
        }
        if (this.ˎ != null) {
            this.ˎ.ˊ(str, i);
            ll.ˏ(ContentManager.getInstant().getContext()).ˋ(new Intent(a.ˎ));
        }
    }
}
